package com.qzy.servlet;

import com.qzy.utils.VerifyCode;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @描述
 * @创建人 joy_qiu
 * @创建时间 2019-12-30-16:14
 * @修改人和其它信息
 */
@WebServlet("/imageServlet")
public class ImageServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response){
        // 输出验证码图片到客户端
        ServletOutputStream out = null;
        VerifyCode verifyCode = new VerifyCode();
        BufferedImage bufferedImage = verifyCode.getImage();
        System.out.println("verifyCode="+verifyCode.getText());
        try {
            out = response.getOutputStream();
            response.setContentType("image/jpeg");
            ImageIO.write(bufferedImage, "jpeg", out);
        } catch (Exception e) {
            System.out.println("create captcha error...!"+e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request,response);
    }
}
